import PureUnitConfig from '../components/unit-config';
import { getFetchStatus } from 'actions/util';
import * as RunnableUnitAction from 'actions/runnable-unit';
import * as ReactRedux from 'react-redux';

const mapStateToProps = (state, ownProps) => {
  const unitId = ownProps.unitId;
  const unit = state.runnableUnitsById[unitId];
  let title;
  if (unit.type === 'MAVEN_SUB') {
    const parentUnit = state.runnableUnitsById[unit.parentId];
    title = `${parentUnit.key}/${unit.url}`;
  } else {
    title = unit.key;
  }

  return {
    typeUpdateFetching: getFetchStatus(state, RunnableUnitAction.updateUnitType),
    unit,
    title
  };
};

const mapDispatchToProps = (dispatch, ownProps) => {
  const unitId = ownProps.unitId;
  return {
    onTypeChange: (type) => {
      return dispatch(RunnableUnitAction.updateUnitType(unitId, type));
    }
  };
};

export default ReactRedux.connect(mapStateToProps, mapDispatchToProps)(PureUnitConfig);
